package com.example.myactivitybase;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

import com.example.myactivitybase.consts.ActionFlag;
import com.example.myactivitybase.receiver.DynamicReceiver;
import com.example.myactivitybase.service.MyService;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, TAG + "-----> onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 动态注册广播接收者
        DynamicReceiver dynamicReceiver = new DynamicReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ActionFlag.DYNAMIC_RECEIVER_FLAG);
        registerReceiver(dynamicReceiver, intentFilter, Context.RECEIVER_NOT_EXPORTED);
    }

    @Override
    protected void onStart() {
        Log.d(TAG, TAG + "-----> onStart()");
        super.onStart();
    }

    @Override
    protected void onRestart() {
        Log.d(TAG, TAG + "-------> onRestart()");
        super.onRestart();
    }

    @Override
    protected void onResume() {
        Log.d(TAG, TAG + "--------> onResume()");
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.d(TAG, TAG + "------> onPause()");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.d(TAG, TAG + "------> onStop()");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, TAG + "------> onDestroy()");
        super.onDestroy();
    }

    /**
     * About 按钮点击事件
     * @param view 视图对象
     */
    public void aboutBtnClick(View view) {
        Intent intent = new Intent(this, AboutActivity.class);
        // 启动另一个Activity
        startActivity(intent);
    }

    /**
     * 启动服务
     * @param view 视图对象
     */
    public void startService(View view) {
        startService(new Intent(this, MyService.class));
    }

    /**
     * 停止服务
     * @param view 视图对象
     */
    public void stopService(View view) {
        stopService(new Intent(this, MyService.class));
    }

    /**
     * ServiceConnection 对象
     * Service 与 Activity 之间的桥梁
     */
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    public void bindService(View view) {
        bindService(new Intent(this, MyService.class), connection, Context.BIND_AUTO_CREATE);
    }

    /**
     * 解绑服务
     * 这里只是测试写法，开发过程中在 Activity 生命周期 Destroy() 中解绑服务
     * @param view view
     */
    public void unbindService(View view) {
        unbindService(connection);
    }

    /**
     * 发送广播
     * @param view view
     */
    public void sendReceiver(View view) {
        Intent intent = new Intent();
        // ActionFlag 需要与注册时保持一致
        intent.setAction(ActionFlag.RECEIVER_FLAG);
        sendBroadcast(intent);
    }

    /**
     * 发送广播
     * 动态注册广播接收者
     * @param view view
     */
    public void sendReceiver2(View view) {
        Intent intent = new Intent();
        intent.setAction(ActionFlag.DYNAMIC_RECEIVER_FLAG);
        sendBroadcast(intent);
    }
}